﻿using System.Windows;
using Models;

namespace SuperQuery
{
    /// <summary>
    /// Interaction logic for EditDatabase.xaml
    /// </summary>
    public partial class EditDatabase : Window, Views.IEditDatabaseView
    {
        
        /// <summary>
        /// Initialise the Edit Database window
        /// </summary>
        /// <param name="databaseToEdit">The database model to edit</param>
        public EditDatabase(Database databaseToEdit)
            : this()
        {
            // Set the datacontext for data binding to work
            DataContext = this;

            m_Presenter = new Presenters.EditDatabasePresenter(this, databaseToEdit);

            // Load the user interface
            editDatabaseContent.Content = m_Presenter.UserInterface;

            // Populate the user interface
            m_Presenter.LoadData();
        }

        /// <summary>
        /// Default constructor for the Edit Databases window
        /// </summary>
        private EditDatabase()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The user has finised editing the database and has chosen to save the changes
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (m_Presenter.ValidateDatabase())
            {
                m_Presenter.UpdateLiveDatabase();
                Close();
            }
        }

        /// <summary>
        /// The Edit Database process has been cancelled so close without doing anything
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// The presenter for the Edit Database window
        /// </summary>
        private readonly Presenters.EditDatabasePresenter m_Presenter;
    }
}
